/***************************************************************************
*			Title: STYL Construction Files
*			Output:
*			Date:
****************************************************************************/


		gen drugsell2w_b= .
		gen time_decl_waitexam3dys_e=.

	drop surveylength_b

//  CLEANING IMPOSSIBLE VALUES

	* Negative Value
		replace landtown_e=1 if landtown_e==-1
	* the question was how many time did you eat? and they answer how much they paid. we replace with missing
		replace foodckshprice3dx_e = . if foodckshprice3dx_e == 50 | foodckshprice3dx_e ==100
		replace foodckshpgb3dx_e = . if foodckshpgb3dx_e == 60 | foodckshpgb3dx_e==125
	* number of room. Obviously a wrong value
		replace rooms_e="5" if rooms_e=="5 rooms"
		destring rooms_e, replace force
		replace rooms_e = . if rooms_e ==560
	* nb of hours they spent selling drugs.
		replace drugsell4whrs_b= . if drugsell4whrs_b==6000
	* prostitution
		replace prostitution2wx_e="10" if prostitution2wx_e=="10+"
		destring prostitution2wx_e, replace force

// CONVERSION TO USD

	foreach x in shop4wprof table4wprof buysell4wprof pettytrade4wprof trade4wprof office4wprof carload4wprof ///
		carwash4wprof moto4wprof carryload4wprof guard4wprof houseclswp4wprof scavenge4wprof beg4wprof farm4wprof  ///
		drugsell4wprof stealing4wprof contract4wprof gambling4wprof smallbiz4wprof constr4wprof fish4wprof  ///
		goldrubber4wprof drinkboozedamt drinkbeerwinexamt drinkginxamt druggrassdamt drugntgrss4wamt drugntgrss7damt ///
		savingsamt savesusuamt saveclubamt savebankamt savewpersonamt savewselfamt saveothamt     ///
		shop4wcash shop7dcash shop1dcash save4w cash7d save7d table4wcash buysell4wcash buysell7dcash buysell1dcash ///
		pettytrade4wcash pettytrade7dcash pettytrade1dcash trade4wcash trade7dcash trade1dcash office4wcash ///
		office7dcash office1dcash carload4wcash carload7dcash carload1dcash carwash4wcash scavenge1dcash   ///
		carwash7dcash carwash1dcash moto4wcash moto7dcash moto1dcash carryload4wcash carryload7dcash  ///
		carryload1dcash guard4wcash guard7dcash guard1dcash houseclswp4wcash scavenge4wcash scavenge7dcash  ///
		farm4wcash farm7dcash farm1dcash drugsell4wcash drugsell7dcash drugsell1dcash contract4wcash contract7dcash   ///
		cash4w  stealing4wcash stealing7dcash  stealing1dcash gambling4wcash  gambling7dcash  ///
		gambling1dcash smallbiz4wcash  smallbiz7dcash smallbiz1dcash constr4wcash constr7dcash constr1dcash  ///
		fish4wcash fish7dcash fish1dcash goldrubber4wcash goldrubber7dcash goldrubber1dcash   ///
		beg4wcash beg7dcash beg1dcash   contract1dcash	other4wcash other7dcash other1dcash {
			gen `x'usd_b = `x'_b if `x'unit_b ==2
			replace `x'usd_b = `x'_b/70 if `x'unit_b ==1
			move `x'usd_b `x'_b
	}


	foreach x in ptrn4wamt ptrneveramt contract4wcash contract7dcash contract7dprof contractprev7dcash contractprev7dprof contract2winventoryamt  ///
		contract2wequipmentamt contract2wmaterialsamt contract2wbizstructamt contractmercsval contractequipval contractrawmatval contractstructval  ///
		inventoryamt inventory7damt materialsamt equipmentamt equipmentothamt bizrentamt bizrent1mcost bizstorageamt bizstructamt bizfeesamt bizsectyamt ///
		bizemployeesamt bizcorruptionamt biztransportamt bizotheramt drinkboozedamt druggrassdamt drugntgrssdamt drugtablet4wamt drughard4wamt  ///
		savesusuamt saveclubamt savebankamt savewpersonamt savewselfamt saveothamt saveuseamt cash4w cash7d save7d cashprev7d saveprev7d  ///
		houseclswpmercsval buysell4wcash  drugsell7dprof save4w shopequipval buysell7dcash buysell7dprof tableprev7dcash tradeprev7dprof ///
		shop7dcash shop7dprof shopprev7dcash shopprev7dprof shop4wcash shopmercsval shoprawmatval shopstructval table7dcash table7dprof  ///
		tableprev7dprof table4wcash tablemercsval tableequipval tablerawmatval tablestructval buysellprev7dcash buysellprev7dprof buysellmercsval  ///
		buysellequipval buysellrawmatval buysellstructval pettytrade7dcash pettytrade7dprof pettytradeprev7dprof pettytrade4wcash pettytrademercsval ///
		tradeequipval scavengeprev7dcash pettytradeprev7dcash tradeprev7dcash officeprev7dcash officeprev7dprof  carloadequipval ///
		pettytradeequipval pettytraderawmatval pettytradestructval trade7dcash trade7dprof traderawmatval tradestructval office7dcash office7dprof  ///
		office4wcash officemercsval trade4wcash trademercsval houseclswpprev7dcash   officeequipval officerawmatval officestructval carloadmercsval  ///
		carload7dcash carload7dprof carloadprev7dcash carloadprev7dprof carload4wcash carloadrawmatval carloadstructval carwash7dcash  ///
		carwashequipval carwashrawmatval carryload7dcash carryload7dprof guard7dprof guardprev7dcash  houseclswpprev7dprof houseclswp4wcash   ///
		carwash7dprof carwashprev7dcash carwashprev7dprof carwash4wcash carwashmercsval carwashstructval moto7dcash moto7dprof motoprev7dcash  ///
		motoprev7dprof moto4wcash motomercsval motoequipval motorawmatval motostructval carryloadprev7dcash carryloadprev7dprof carryload4wcash  ///
		carryloadmercsval carryloadequipval carryloadrawmatval carryloadstructval guard7dcash guardprev7dprof guard4wcash guardmercsval  ///
		guardequipval guardrawmatval guardstructval houseclswp7dcash houseclswp7dprof houseclswpequipval houseclswprawmatval houseclswpstructval  ///
		scavenge7dcash scavenge7dprof beg7dprof begprev7dcash farmmercsval farmequipval drugsellrawmatval stealingstructval ///
		scavengeprev7dprof scavenge4wcash scavengemercsval begprev7dprof beg4wcash scavengeequipval scavengerawmatval scavengestructval beg7dcash  ///
		begmercsval begequipval begrawmatval begstructval farmrawmatval farmstructval farm7dcash farm7dprof farmprev7dcash farmprev7dprof farm4wcash ///
		drugsell7dcash drugsellprev7dcash drugsellstructval     drugsellprev7dprof drugsell4wcash drugsellmercsval drugsellequipval  ///
		shop2winventoryamt shop2wequipmentamt shop2wmaterialsamt shop2wbizstructamt stealingmercsval  stealingequipval  stealingrawmatval   ///
		stealing4wcash  stealing7dcash  stealing7dprof  stealingprev7dcash  stealingprev7dprof stealing2winventoryamt  stealing2wequipmentamt   ///
		gambling4wcash  gambling7dcash  gambling7dprof  gamblingprev7dcash  gamblingprev7dprof  gambling2winventoryamt  gambling2wequipmentamt   ///
		gambling2wmaterialsamt  gambling2wbizstructamt stealing2wmaterialsamt  stealing2wbizstructamt farm2wbizstructamt drugsell2winventoryamt  ///
		gamblingmercsval  gamblingequipval  gamblingrawmatval  gamblingstructval  goldrubberrawmatval  goldrubberstructval  other4wcash   ///
		smallbiz4wcash  smallbiz7dcash  smallbiz7dprof  smallbizprev7dcash  smallbizprev7dprof	smallbiz2winventoryamt  smallbiz2wequipmentamt  ///
		smallbiz2wmaterialsamt  smallbiz2wbizstructamt constrprev7dprof buysell2wmaterialsamt buysell2wbizstructamt ///
		smallbizmercsval  smallbizequipval  smallbizrawmatval  smallbizstructval constr4wcash  constr7dcash  constr7dprof  constrprev7dcash   ///
		constr2winventoryamt  constr2wequipmentamt  constr2wmaterialsamt  constr2wbizstructamt  constrmercsval  ///
		constrequipval  constrrawmatval  constrstructval  goldrubber4wcash  goldrubber7dcash  goldrubber7dprof ///
		fish4wcash   fish7dcash  fish7dprof  fishprev7dcash  fishprev7dprof  fish2winventoryamt  fish2wequipmentamt  ///
		fish2wmaterialsamt  fish2wbizstructamt  fishmercsval  fishequipval  fishrawmatval  fishstructval  ///
		goldrubberprev7dcash  goldrubberprev7dprof  goldrubber2winventoryamt  goldrubber2wequipmentamt  ///
		goldrubber2wmaterialsamt  goldrubber2wbizstructamt  goldrubbermercsval  goldrubberequipval  ///
		other7dcash  other7dprof  otherprev7dcash  otherprev7dprof  other2winventoryamt  other2wequipmentamt  ///
		other2wmaterialsamt  other2wbizstructamt  othermercsval  otherequipval  otherrawmatval  otherstructval  ///
		table2winventoryamt table2wequipmentamt table2wmaterialsamt table2wbizstructamt buysell2winventoryamt buysell2wequipmentamt  ///
		pettytrade2winventoryamt pettytrade2wequipmentamt pettytrade2wmaterialsamt pettytrade2wbizstructamt trade2winventoryamt trade2wequipmentamt  ///
		office2winventoryamt office2wequipmentamt office2wmaterialsamt office2wbizstructamt carload2winventoryamt carload2wequipmentamt  ///
		carload2wmaterialsamt carload2wbizstructamt trade2wmaterialsamt trade2wbizstructamt scavenge2wmaterialsamt scavenge2wbizstructamt ///
		carwash2winventoryamt carwash2wequipmentamt carwash2wmaterialsamt carwash2wbizstructamt moto2winventoryamt moto2wequipmentamt  ///
		moto2wmaterialsamt moto2wbizstructamt carryload2winventoryamt guard2wbizstructamt houseclswp2winventoryamt  ///
		carryload2wequipmentamt carryload2wmaterialsamt carryload2wbizstructamt guard2winventoryamt guard2wequipmentamt guard2wmaterialsamt  ///
		houseclswp2wequipmentamt houseclswp2wmaterialsamt houseclswp2wbizstructamt scavenge2winventoryamt scavenge2wequipmentamt  ///
		beg2winventoryamt beg2wequipmentamt beg2wmaterialsamt beg2wbizstructamt farm2winventoryamt farm2wequipmentamt farm2wmaterialsamt  ///
		drugsell2wequipmentamt drugsell2wmaterialsamt drugsell2wbizstructamt     {
			gen `x'usd_e = `x'_e if `x'unit_e ==2
			replace `x'usd_e = `x'_e/70 if `x'unit_e ==1
			move `x'usd_e `x'_e
         cap label variable `x'usd_e `"`: variable label `x'_e' (USD)"'

	}

	* la var
		la var cash4wusd_b "How much cash did you get from work in total in the last 4 weeks (USD)?"
		la var cash7dusd_b "How much cash did you get from work in total in the last 7days (USD)?"

	* Rename savings variables
		ren save7dusd_e saveusd7d_e
		ren saveprev7dusd_e saveusdprev7d_e

	* For phase 2 Baseline and phase 1 3w, we asked directly the LD amount
		replace druggrassdamtusd_b = druggrassdamtld_b/70 if p2==1
		replace druggrassdamtusd_e = druggrassdamtld_e/70 if p1_e3wks ==1


// CONVERT TIME VARIABLES  HOURS MINUTES AM/PM

	* Conversion from "9:07 AM" to 3 variables Hours, Minutes, AM/PM
		foreach var in wg_g1h  wg_cclfinaltime wg_cclwatertime {
			split  `var'_b, parse(:) gen(`var'_b)
			split  `var'_b2, parse() gen(`var'_b2)
			destring  `var'_b1 `var'_b21, replace
			drop  `var'_b2
			ren `var'_b1 `var'h_b
			ren `var'_b21 `var'm_b
			ren  `var'_b22  `var'apm_b
			move `var'h_b `var'_b
			move  `var'm_b  `var'_b
			move  `var'apm_b  `var'_b
		}

	* Conversion from "12/30/1899 9:59:00" to Hours, Minutes, AM/PM

		* Endline
		foreach x in  dg_time1 dg_time2 sdchoosechecktime dg_starttime datestart cclendtime {
			split `x'_e, parse()
			split `x'_e2, parse(:)

			ren `x'_e21 `x'h_e
			ren `x'_e22 `x'm_e

			destring `x'h_e `x'm_e, replace

			move `x'h_e `x'_e
			move `x'm_e `x'_e

			drop  `x'_e2 `x'_e23 `x'_e1
		}

		* Baseline
		foreach x in  sdchoosechecktime datestart timeend {
			split `x'_b, parse()
			split `x'_b2, parse(:)

			ren `x'_b21 `x'h_b
			ren `x'_b22 `x'm_b

			destring `x'h_b `x'm_b, replace

			move `x'h_b `x'_b
			move `x'm_b `x'_b

			drop  `x'_b2 `x'_b23 `x'_b1
		}

	* Clean gstarttime_b

		* p1 looks like this 10:02 AM

			split  gstarttime_b if p1==1 , parse(:) gen(gstarttime_b)
			split  gstarttime_b2 if p1==1, parse() gen(gstarttime_b2)
			destring  gstarttime_b1 gstarttime_b21, replace
			ren  gstarttime_b22  gstarttimeapm_b
			ren gstarttime_b1 gstarttimeh_b
			ren gstarttime_b21 gstarttimem_b
			drop  gstarttime_b2
			move gstarttimeh_b   gstarttime_b
			move  gstarttimem_b  gstarttime_b
			move  gstarttimeapm_b  gstarttime_b

		* p2 looks like this: 12/30/1899 9:37:00

			split gstarttime_b if p2==1, parse() gen(gstarttime_b_2)
			split gstarttime_b_22 if p2==1, parse(:) gen(gstarttime_b_2_2)
			destring gstarttime_b_2_21 gstarttime_b_2_22, replace
			move  gstarttime_b_2_21 gstarttime_b
			move gstarttime_b_2_22 gstarttime_b
			replace gstarttimeh_b = gstarttime_b_2_21 if p2==1
			replace gstarttimem_b = gstarttime_b_2_22 if p2==1

			replace gstarttimeapm_b ="AM" if gstarttimeh_b ==9 | gstarttimeh_b ==10 | gstarttimeh_b ==11

			replace gstarttimeapm_b = "PM" if gstarttimeh_b ==12 | gstarttimeh_b ==13 | gstarttimeh_b ==14 |gstarttimeh_b ==15 | gstarttimeh_b ==16 | gstarttimeh_b ==17 |gstarttimeh_b ==18
			replace gstarttimeh_b = 1 if gstarttimeh_b ==13
			replace gstarttimeh_b = 2 if gstarttimeh_b ==14
			replace gstarttimeh_b = 3 if gstarttimeh_b ==15
			replace gstarttimeh_b = 4 if gstarttimeh_b ==16
			replace gstarttimeh_b = 5 if gstarttimeh_b ==17
			replace gstarttimeh_b = 6 if gstarttimeh_b ==18

			drop gstarttime_b_21  gstarttime_b_2_23 gstarttime_b_22
			drop gstarttime_b_2_21 gstarttime_b_2_22 gstarttime_b

	* Conversion from "1:02:54" to 3 variables Minutes, Second, Millisecondes

		* In this case, we already created the variables `x'm_b `x's_b `x'ms_b in phase2, we just have to populate them
			foreach x in  g4b g4d g4f g2c g3m1a g3m1e g3m2a g3m2e g3m3a g3m3e g5g1e g5g2e g5g3e g5g4e g5g5e{
				split `x'_b , parse(:)
				destring `x'_b1 `x'_b2 `x'_b3, replace
				replace `x'm_b = `x'_b1 if p1==1
				replace `x's_b = `x'_b2 if p1==1
				replace `x'ms_b = `x'_b3 if p1==1
				drop `x'_b `x'_b1 `x'_b2 `x'_b3
			}

		* In this case, phase 2 entered the number of second while in phase 1 we entered "0:16:06"
			split g6c_b if p1==1, parse(:)
			replace g6c_b = "" if  p1==1
			destring g6c_b g6c_b1  g6c_b2  g6c_b3, replace
			replace  g6c_b = 60 if g6c_b1== 1 & p1==1
			replace  g6c_b = g6c_b2 if p1==1 & g6c_b1==0
			drop g6c_b1 g6c_b2 g6c_b3


	// GENERATE  `x'sec

		* Here we create the variables `x'sec which is equal to min*60 + sec + millisec/1000

			* Endline
			foreach x in g3m1a g3m1e g3m2a g3m2e g3m3a g3m3e {

				gen  `x'mtos_e =  `x'm_e *60
				gen  `x'mstos_e  =  `x'ms_e /1000

				egen  `x'sec_e  = rowtotal ( `x'mtos_e   `x's_e   `x'mstos_e )
				replace `x'sec_e  = . if  `x'm_e  ==. &  `x's_e  ==.
				drop  `x'mtos_e   `x'mstos_e

				egen `x'secstd_e  =std(`x'sec_e )
			}

			* Baseline
			foreach x in g5g1e  g5g2e g5g3e g5g4e g5g5e g3m1a g3m1e g3m2a g3m2e g3m3a g3m3e  {

				gen  `x'mtos_b =  `x'm_b*60
				gen  `x'mstos_b =  `x'ms_b/1000

				egen  `x'sec_b = rowtotal ( `x'mtos_b  `x's_b  `x'mstos_b)
				replace `x'sec_b = . if  `x'm_b ==. &  `x's_b ==.
				drop  `x'mtos_b  `x'mstos_b

				egen `x'secstd_b =std(`x'sec_b)
			}

** SURVEY AND GAME START AND END **

	* Some missing observation
		tab1  p1_e3wks p1_e5m p1_e7m p1_e12m p1_e13m p2_e2wks p2_e2w2w p2_e12m  p3_e2w p3_e5w if datestart_e==""
		tab1  p1_e3wks p1_e5m p1_e7m p1_e12m p1_e13m p2_e2wks p2_e2w2w  p2_e12m  p3_e2w p3_e5w if cclendtime_e==""

	// Compute survey start and survey end
		* GENERATE  `x'min: Number of Minutes after  00:00
		* NOTE TO ME:  12 PM midday

		* Survey Start
		 	gen surveystart_e =  datestarth_e*60 +  datestartm_e
				la var surveystart_e "Survey Start (Minutes after 00:00)"

			gen surveystart_b =  datestarth_b*60 +  datestartm_b
				la var surveystart_b "Survey Start (Minutes after 00:00)"

		* Survey End
			gen surveyend_e =  cclendtimeh_e*60 +  cclendtimem_e
				la var surveyend_e "Survey Start (Minutes after 00:00)"

			gen surveyend_b =  timeendh_b*60 +  timeendm_b
				la var surveyend_b "Survey Start (Minutes after 00:00)"

		* Survey Length
			gen surveylength_b =surveyend_b-surveystart_b
			replace surveylength_b = . if surveylength_b <0
				la var surveylength_b "Baseline Survey Length"

			gen surveylength_e =surveyend_e-surveystart_e
			replace surveylength_e = . if surveylength_e <0
				la var surveylength_e "Endline Survey Length"

		* Cap the survey length. The highest value are probably wrong due to resume .
		replace surveylength_e = . if surveylength_e >300
		replace surveylength_b = . if surveylength_b >300

		* Game Start
			gen gamestart_b =  gstarttimeh_b*60 +  gstarttimem_b if gstarttimeapm_b == "AM" | (gstarttimeapm_b == "PM" & gstarttimeh_b == 12)
			replace gamestart_b = (gstarttimeh_b+12)*60 +  gstarttimem_b if gstarttimeapm_b == "PM"  & gstarttimeh_b != 12
				la var gamestart_b "Games Start (Minutes after 00:00)"

		* Game End


** BASELINE DEMOGRAPHICS **

	* Age

		gen age_b=ageexact_b
		replace age_b = 18 if agerange_b=="15-20" & age_b==.
		replace age_b = 23 if agerange_b=="21-25" & age_b==.
		replace age_b = 28 if agerange_b=="26-30" & age_b==.
		replace age_b = 33 if agerange_b=="31-35" & age_b==.
		replace age_b = 38 if agerange_b=="36-40" & age_b==.
		replace age_b = 43 if agerange_b=="41-45" & age_b==.

		replace age_b = 2011 - ageyear_b if ageyear_b == 2000 & age_b==. & p1==1
			la var age_b "Age"

	* Physical Health

		egen health_b = rowtotal(walk_b carry_b brushallday_b)
		replace health_b=health_b-3
			la var health_b "Index of health (difficulty with daily life activities), 0-6, additive bad"

		gen health_resc_b = health_b*-1 +6
			la var health_resc_b "Index of health (difficulty with daily life activities), 0-6, additive good"

		summ health_resc_b
		gen health_resc_z_b = (health_resc_b-r(mean))/r(sd)
			la var health_resc_z_b "Index of health"


	* Skills training (not in p3)

			sum  train1_b train1time_b train1timeunit_b train2_b train2time_b train2timeunit_b trainbiz_b trainbiztime_b trainbiztimeunit_b

		* Conversion to months of training
			foreach x in train1 train2 trainbiz {
				gen `x'_mo_b = `x'time_b if `x'time_b!=. & `x'timeunit_b==3
				replace  `x'_mo_b = `x'time_b/4 if `x'time_b!=. & `x'timeunit_b==2
				replace `x'_mo_b = `x'time_b*12 if `x'time_b!=. & `x'timeunit_b==4
			}

			la var train1_mo_b "Training 1: Months of training"
			la var train2_mo_b "Training 2: Months of training"
			la var trainbiz_mo_b "Training Biz: Months of training"


		* Check visually that all survey are coded the right way
			* 1-Carpentry, 2- Masonry, 3- Mechanics, 4- Tailoring, 5- Driving, 6- Computers, 7- Agriculture, 8- Tie Dye, 9- Soap making, 10-Cooking/Baking, 11-Hair-dressing,  12-Plumbing, 13-Electrician, 14-Metal work, 15-Shoe making, 96-Other

			ta train1course_b
			ta train1course_b, nol
			ta train2course_b
			ta train2course_b, nol
			ta trainbizcourse_b
			ta trainbizcourse_b, nol

		* Clean other categories

			tab1 train1courseoth_b train2courseoth_b  trainbizcourseoth_b

			replace train1course_b = 13 if train1courseoth_b=="ELECTRIC WELDING" & train1course_b== .o
			replace train1courseoth_b = "" if train1courseoth_b=="ELECTRIC WELDING"

			replace train1course_b = 10 if train1courseoth_b=="Baking bread" & train1course_b== .o
			replace train1courseoth_b = "" if train1courseoth_b=="Baking bread"

			replace train1course_b = 10 if train1courseoth_b=="Calvering work" & train1course_b== .o
			replace train1courseoth_b = "" if train1courseoth_b=="Calvering work"

			replace train1course_b = 3 if train1courseoth_b=="Repair car tyre" & train1course_b== .o
			replace train1courseoth_b = "" if train1courseoth_b=="Repair car tyre"

			replace train1course_b = 14 if train1courseoth_b=="Gold smith"  & train1course_b== .o
			replace train1courseoth_b = "" if train1courseoth_b=="Gold smith"

			replace train2course_b = 10 if train2courseoth_b  =="ICE CREAM PRODUCTION"
			replace train2courseoth_b = "" if train2courseoth_b  =="ICE CREAM PRODUCTION"

		* Check for duplcates or irrelevant training
			replace train2_b =0 if partid == 74572726
			replace train2course_b =. if partid == 74572726
			replace train2courseoth_b ="" if partid == 74572726
			replace train2_mo_b =. if partid == 74572726



		* Construction total months of training
			egen train_mo_b = rowtotal( train1_mo_b train2_mo_b trainbiz_mo_b), mi
			replace train_mo_b = 0 if train_mo_b ==. & (p1==1 ) & (train1_b !=.| train2_b!=. |  trainbiz_b!= .)
			replace train_mo_b = 0 if train_mo_b ==. & (p2==1 ) & (train1_b !=.| train2_b!=. |  trainbiz_b!= .)
				la var train_mo_b "Months of training"

			gen train_yr_b = train_mo_b/12
				la var  train_yr_b "Years of training"

		* Capp
			sum train_mo_b, d
			replace train_mo_b=48 if train_mo_b!=. & train_mo_b>48

	* Formal education
		* NOTE: ABC-K2 given value 0

		gen school_b = 0 if schoolbasstop_b>=0 & schoolbasstop_b<=3
		replace school_b = schoolbasstop_b-3 if schoolbasstop_b>=4 & schoolbasstop_b<=15
		replace school_b = 12 if schoolbasstop_b==16
		replace school_b = 12 if schoolbasstop_b ==17 & schoolbasuniv_b ==0
		replace school_b = 13 if schoolbasstop_b ==17 & schoolbasuniv_b ==1
		replace school_b = 14 if schoolbasstop_b ==17 & schoolbasuniv_b ==2
		replace school_b = 15 if schoolbasstop_b ==17 & schoolbasuniv_b ==3
		replace school_b = 16 if schoolbasstop_b ==17 & schoolbasuniv_b ==4

		replace school_b = 16 if school_b==. & schoolbaslevel_b ==17 & schoolbasuniv_b ==4
		replace school_b = 15 if school_b==. & schoolbaslevel_b ==17 & schoolbasuniv_b ==3
		replace school_b = 14 if school_b==. & schoolbaslevel_b ==17 & schoolbasuniv_b ==2
		replace school_b = 13 if school_b==. & schoolbaslevel_b ==17 & schoolbasuniv_b ==1
		replace school_b = 12 if school_b==. & schoolbaslevel_b==15
		replace school_b = 11 if school_b==. & schoolbaslevel_b==14
		replace school_b = 10 if school_b==. & schoolbaslevel_b==13
		replace school_b = 9 if school_b==. & schoolbaslevel_b==12
		replace school_b = 8 if school_b==. & schoolbaslevel_b==11
		replace school_b = 7 if school_b==. & schoolbaslevel_b==10
		replace school_b = 6 if school_b==. & schoolbaslevel_b==9
		replace school_b = 5 if school_b==. & schoolbaslevel_b==8
		replace school_b = 4 if school_b==. & schoolbaslevel_b==7
		replace school_b = 3 if school_b==. & schoolbaslevel_b==6
		replace school_b = 2 if school_b==. & schoolbaslevel_b==5
		replace school_b = 1 if school_b==. & schoolbaslevel_b==4
			la var school_b "Years of Schooling"



	* Educational attainment

		egen educ_b = rowtotal (school_b train_yr_b) if train_mo_b!=.
			la var educ_b "Educational attainment (years, formal and informal)"

	*  Math Text

		gen matheasycorrect_b = (matheasy_b== 13)
			la var matheasycorrect_b "Math: Answered 13 for 10 + 3"

		gen addcorrect_b=(mathadd_b==57)
			la var addcorrect_b "Math: Answered 57 for 21 + 36"

		gen subcorrect_b=( mathsubtract_b==7)
			la var subcorrect_b "Math: Answered 7 for 16 - 9"

		gen multcorrect_b=(mathmultiply_b==50)
			la var multcorrect_b "Math: Answered 50 for 5 x 10"

		gen mult2correct_b=(mathmultiply2_b==77)
			la var mult2correct_b "Math: Answered 77 for 7 x 11"

		gen inccorrect_b=(mathincome_b==120)
			la var inccorrect_b "Math: Answered 120 for $10/mo for 12 mo"

		gen intcorrect_b=(mathinterest_b==5)
			la var intcorrect_b "Math: Answered 5 for 10% interest/mo on 50 LD after 1 mo"

		egen mathscore_b = rowtotal(addcorrect_b subcorrect_b multcorrect_b mult2correct_b inccorrect_b)
		replace mathscore_b = . if addcorrect_b==.& subcorrect_b==.& multcorrect_b ==.&mult2correct_b==.& inccorrect_b==.
			la var mathscore_b "Math: additive index (5 questions)"

	// TIME IN MONROVIA
		gen monroviayears_b = monroviatime_b
			replace monroviayears_b = monroviayears_b/365 if monroviatimeunit_b == 1
			replace monroviayears_b = monroviayears_b/12 if monroviatimeunit_b == 3
			replace monroviayears_b = age_b if monrovia_b == 1 & missing(monroviayears_b)
			la var monroviayears_b "Number of years living in Liberia"


	// MISCELLANEOUS

		gen spongematress_b = (ownbed_b ==1) if ownbed_b !=.
		gen livewife_b = (mmarriedlive_b==1) if mmarried_b !=.
		gen livepartner_b = (mmarriedlive_b==1 | mgirlfriendlive_b==1)
		replace livepartner_b =. if mmarried_b ==. & mgirlfriendlive_b==.
			la var livepartner_b "Married or living with a partner"
		gen fightphys4w_dum_b = (fightphys4w_b!=0) if fightphys4w_b!=.
		replace fightphys4w_dum_b = 1 if fights_b != 0 & p1==1 & fightphys4w_dum_b==.

		gen muslim_b=0
			replace muslim_b=1 if rel_b==2
			la var muslim_b "Muslim"



	** ALIVE LADDERS **

	// ALive Ladders

		* BASELINE (p2)
		tab1  aliveage40_b aliveage45_b aliveage50_b aliveage55_b aliveage60_b aliveage65_b aliveage70_b aliveage75_b aliveage80_b aliveage85_b aliveage90_b aliveage95_b aliveage100_b if surveyed_p2_r1_b==1

		gen aliveladder_b = 100 if aliveage100_b ==1 &  p2==1
		replace aliveladder_b = 95 if aliveage95_b ==1 & aliveladder_b==. &  p2==1
		replace aliveladder_b = 90 if aliveage90_b ==1 & aliveladder_b==.  & p2==1
		replace aliveladder_b = 85 if aliveage85_b ==1 & aliveladder_b==. & p2==1
		replace aliveladder_b = 80 if aliveage80_b ==1 & aliveladder_b==. & p2==1
		replace aliveladder_b = 75 if aliveage75_b ==1 & aliveladder_b==. & p2==1
		replace aliveladder_b = 70 if aliveage70_b ==1 & aliveladder_b==. & p2==1
		replace aliveladder_b = 65 if aliveage65_b ==1 & aliveladder_b==. & p2==1
		replace aliveladder_b = 60 if aliveage60_b ==1 & aliveladder_b==. & p2==1
		replace aliveladder_b = 55 if aliveage55_b ==1 & aliveladder_b==. & p2==1
		replace aliveladder_b = 50 if aliveage50_b ==1 & aliveladder_b==. & p2==1
		replace aliveladder_b = 45 if aliveage45_b ==1 & aliveladder_b==. & p2==1
		replace aliveladder_b = 40 if aliveage40_b ==1 & aliveladder_b==. & p2==1

	 	ta aliveladder_b if surveyed_p2_r1_b==1


 ** HEALTH **

		gen disabledlostarm_b =(disabledspecify_b_1==1) if disabled_b!=.
		replace disabledlostarm_b = 1 if (disabledspecifyoth_b=="Sorter left arm"  | disabledspecifyoth_b=="3 fingers off with broken left hand"  | disabledspecifyoth_b=="Bullet passed through the arm causing mad function of the hand." |  disabledspecifyoth_b=="Finger got broken before and cannot function now" )
		replace disabledspecifyoth_b = "" if disabledspecifyoth_b=="Sorter left arm"  | disabledspecifyoth_b=="3 fingers off with broken left hand"  | disabledspecifyoth_b=="Bullet passed through the arm causing mad function of the hand." |  disabledspecifyoth_b=="Finger got broken before and cannot function now"
			la var disabledlostarm_b "Disabled: Lost Arm"

		gen disabledlostleg_b =(disabledspecify_b_1==2) if disabled_b!=.
		replace disabledlostleg_b = 1 if  disabledspecifyoth_b=="Rocket  mark on leg painful"  | disabledspecifyoth_b=="One  leg disable from injection"  |  disabledspecifyoth_b=="Using wheel chair as two legs are ineffective" | disabledspecifyoth_b=="Toe cut"
		replace disabledspecifyoth_b = "" if  disabledspecifyoth_b=="Rocket  mark on leg painful"  | disabledspecifyoth_b=="One  leg disable from injection"  |  disabledspecifyoth_b=="Using wheel chair as two legs are ineffective" | disabledspecifyoth_b=="Toe cut"
			la var disabledlostleg_b "Disabled: Lost Leg"

		gen disableddeformation_b =(disabledspecify_b_1==4) if disabled_b!=.
			la var disableddeformation_b "Disabled: Deformation "

		gen disabledblind_b =(disabledspecify_b_1==5 ) if disabled_b!=.
		replace disabledblind_b =1 if disabledspecifyoth_b=="  left eye problem" | disabledspecifyoth_b=="Lost  right eye " | disabledspecifyoth_b=="One eyes effected" | disabledspecifyoth_b=="On of the eyes blind"  | disabledspecifyoth_b=="Right  eye can't see"
		replace disabledspecifyoth_b = "" if disabledspecifyoth_b=="  left eye problem" | disabledspecifyoth_b=="Lost  right eye " | disabledspecifyoth_b=="One eyes effected" | disabledspecifyoth_b=="On of the eyes blind"  | disabledspecifyoth_b=="Right  eye can't see"
			la var disabledblind_b "Disabled: Blind"

		gen disableddeaf_b=(disabledspecify_b_1==6) if disabled_b!=.
			la var disableddeaf_b "Disabled: Deaf/Hearing "

		gen disabledother_b =(disabledspecify_b_1==7) if disabled_b!=.
		replace disabledother_b =1 if disabledspecifyoth_b=="Serious heart problem" |  disabledspecifyoth_b=="Suffers from cold."  |  disabledspecifyoth_b=="Serious headake"  |disabledspecifyoth_b=="Pains in the bones" | disabledspecifyoth_b=="Took  operation" | disabledspecifyoth_b=="Took  operation on stomach" | disabledspecifyoth_b=="Wonds in the ribs"   |disabledspecifyoth_b=="sometimes falls off " | disabledspecifyoth_b=="Tooth ill and stomach ill"  | disabledspecifyoth_b=="Stoma h pain" | disabledspecifyoth_b=="Stomach problem" | disabledspecifyoth_b=="Lost four teeth"
		replace disabledother_b =1 if  disabledspecify_b_1 ==96 & disabledlostarm_b ==0 & disabledlostleg_b ==0 & disableddeformation_b ==0 & disabledblind_b ==0 & disableddeaf_b==0 &  disabledother_b ==0
		replace disabledother_b =1 if disabledspecifyoth_b=="Arm sprung before" | disabledspecifyoth_b=="Foot hearting" | disabledspecifyoth_b=="HE HAS A SOUU HE HAS A SOUL ON HIS LEG THAT WAS GOTTEN WHEN HE STEP ON AFRICAN MEDICINE IN THE INTERIOR"   | disabledspecifyoth_b=="Hydro-seed" | disabledspecifyoth_b=="Karterial in the eye"  | disabledspecifyoth_b=="Spinal cord sprung before"  | disabledspecifyoth_b=="Suffering from pah(from the anus)"
			la var disabledother_b "Disabled: Minor"


 ** WAR EXPERIENCES **

	foreach x in refugee displaced {
		gen `x'years =  `x'x_b if `x'xunit_b ==4
		replace `x'years =  `x'x_b/12 if `x'xunit_b ==3
		replace `x'years =  `x'x_b/52 if `x'xunit_b ==2
		replace `x'years =  `x'x_b/365 if `x'xunit_b ==1
		move `x'years `x'x_b
	}
